import 'dart:math';

import 'package:flutter/material.dart';
import 'package:flutter_house_manager/controller/BuildController.dart';
import 'package:flutter_house_manager/utils/PromptAction.dart';
import 'package:get/get.dart';

class BuildingList extends StatefulWidget {
  const BuildingList({super.key});

  @override
  State<BuildingList> createState() => _RoomListState();
}

class _RoomListState extends State<BuildingList> {
  int size = 0;
  @override
  void didChangeDependencies() {
    // TODO: implement didChangeDependencies
    super.didChangeDependencies();
  }

  BuildController buildController = Get.find();
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    Random r = Random();
    size = r.nextInt(10) + 2;

    // 当 数量小于5的时候 认为是单元
    // 当数量大于等于5的时候 认为是楼栋
    setState(() {});
  }

  Widget getShowText(int index) {
    if (size < 5) {
      return Text(
          buildController.buildInfo["point"] + (index + 1).toString() + '单元');
    }
    return Text(
        buildController.buildInfo["point"] + (index + 1).toString() + '栋');
  }

  @override
  Widget build(BuildContext context) {
    // 使用GetBuilder包裹
    return GetBuilder<BuildController>(builder: (controller) {
      return Scaffold(
        appBar: AppBar(
          title: const Text('选择楼栋'),
        ),
        body: ListView.builder(
          itemBuilder: (BuildContext context, int index) {
            return GestureDetector(
              onTap: () {
                buildController.buildInfo["build"] =
                    (index + 1).toString() + ((size < 5) ? '单元' : "栋");
                buildController.updateBuildInfo(buildController.buildInfo);
                // point 小区信息
                // build 楼栋信息
                Navigator.pushNamed(context, '/roomlist');
              },
              child: Container(
                color: Colors.white,
                padding: const EdgeInsets.all(10),
                child: Row(
                  children: [
                    Expanded(child: getShowText(index)),
                    const Row(
                      children: [
                        Icon(Icons.arrow_forward_ios,
                            size: 16, color: Colors.black),
                      ],
                    )
                  ],
                ),
              ),
            );
          },
          itemCount: size,
        ),
      );
    });
  }
}
